/*******************************************************************************
 * Copyright (c) 2012 March and Conquest.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package mrusanov.towers.resources;

public class ResourceBase {
	protected int bricks;
	protected int gems;
	protected int beasts;

	public int getBricks() {
		return bricks;
	}

	public int getResource(Resource resource) {
		switch (resource) {
			case BEASTS:
				return beasts;
			case BRICKS:
				return bricks;
			case GEMS:
				return gems;
			default:
				throw new IllegalArgumentException("Unknown resource: " + resource);
		}
	}

	public void setResource(Resource resource, int value) {
		switch (resource) {
			case BEASTS:
				setBeasts(value);
				break;
			case BRICKS:
				setBricks(value);
				break;
			case GEMS:
				setGems(value);
				break;
			default:
				throw new IllegalArgumentException("Unknown resource: " + resource);
		}
	}

	public void addBricks(int bricks) {
		this.bricks += bricks;
	}

	public void subtractBricks(int bricks) {
		this.bricks = subtractWithoutBeingNegative(this.bricks, bricks);
	}

	public int getGems() {
		return gems;
	}

	public void addGems(int treasures) {
		gems += treasures;
	}

	public void subtractGems(int treasures) {
		gems = subtractWithoutBeingNegative(gems, treasures);
	}

	public int getBeasts() {
		return beasts;
	}

	public void setBricks(int bricks) {
		this.bricks = bricks;
	}

	public void setGems(int gems) {
		this.gems = gems;
	}

	public void setBeasts(int beasts) {
		this.beasts = beasts;
	}

	public void addBeasts(int beasts) {
		this.beasts += beasts;
	}

	public void subtractBeasts(int beasts) {
		this.beasts = subtractWithoutBeingNegative(this.beasts, beasts);
	}

	private int subtractWithoutBeingNegative(int minuend, int subtrahend) {
		int result = minuend - subtrahend;
		if (result < 0) {
			return 0;
		} else {
			return result;
		}
	}
}
